package com.crawler.domain;

import com.rub.core.AvExecutor;
import com.rub.utils.FileUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VideoStream {

    static String ffmpegCommand = "ffmpeg";
    static String videoPathC = "D:\\BaiduNetdiskDownload\\北京圣思园Java Web培训视频-HK共享吧收集整理\\wmv";

    public static void main(String[] args) {

       /* List<String> strings = FileUtil.listFiles(videoPathC, new ArrayList<>());
        for (String videoPath : strings) {
            int endTime = getVideoTime(videoPath).intValue();
            cutVideo("00:00:12", String.valueOf(endTime), videoPath);
        }*/

        String inputPath = "D:/Program Files/JiJiDown/Download/南宗慧能北神秀，二僧现世化一方。/1.mp4";

        /*delogo(
                inputPath,
                56,
                33,
                247,
                77
        );*/

        // cutVideo("00:00:12", String.valueOf(""), videoPath);

        FileUtil.listFiles(videoPathC, new ArrayList<>()).forEach(file -> {

            new Thread(() -> createImg(file, "jpg")).start();

        });
    }

    public static Double roundTime(Double time) {
        BigDecimal b = new BigDecimal(time);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * ffmpeg -i test.wmv -y test.mp4
     * ffmpeg -i test.wmv -y -qscale 0 -vcodec libx264 test.mp4
     * ffmpeg -i test.wmv -c:v libx264 -strict -2 -y test.mp4
     *
     * @param inputFile
     * @param fileType
     */
    public static void covertVideo(String inputFile, String fileType) {

        boolean slashLeft = inputFile.contains("/");
        //切割路径
        String[] slashArr = null;
        String cutPath = "";
        if (slashLeft) {
            slashArr = inputFile.split("/");
            cutPath = "/cut/";
        } else {
            slashArr = inputFile.split("\\\\");
            cutPath = "\\cut\\";
        }

        String outFileName = slashArr[slashArr.length - 1];
        //拼接新文件名称
        String fileName = String.valueOf(outFileName).split("\\.")[1] + "." + fileType;

        //组装输出路径
        String outPath = inputFile.replace(outFileName, "");

        //去掉最后一个斜杠或反斜杠
        String newPath = !slashLeft ? outPath.substring(0, outPath.lastIndexOf("\\"))
                : outPath.substring(0, outPath.lastIndexOf("/"));

        //输出文件包含目录
        String cutDirs = newPath + cutPath;

        //创建文件目录,不存在也创建
        FileUtil.mkDirs(cutDirs);

        String outFile = cutDirs + fileName;

        List<String> commands = new ArrayList<>();
        commands.add(ffmpegCommand);
        commands.add("-i");
        commands.add(inputFile);
       /* commands.add("-c:v");
        commands.add("libx264");
        commands.add("-strict");
        commands.add("-2");*/
        commands.add("-y");
        commands.add(outFile);

        AvExecutor. runCommand(commands);
    }

    /**
     * ffmpeg -ss 00:00:10 -i 111.mp4 -t 502 -codec copy -y cut_1.mp4
     *
     * @param startTime
     * @param endTime
     * @param inputFile
     */
    public static void cutVideo(String startTime, String endTime, String inputFile) {

        boolean slashLeft = inputFile.contains("/");
        //切割之后得到的文件名
        String fileName = "";
        //切割路径
        String[] slashArr = null;
        String cutPath = "";
        if (slashLeft) {
            slashArr = inputFile.split("/");
            cutPath = "/cut/";
        } else {
            slashArr = inputFile.split("\\\\");
            cutPath = "\\cut\\";
        }
        fileName = slashArr[slashArr.length - 1];

        //输出目录
        String outPath = inputFile.replace(fileName, "");
        //输出文件包含目录
        String outFile = outPath + cutPath + fileName;
        List<String> commands = new ArrayList<>();
        commands.add(ffmpegCommand);
        commands.add("-ss");
        commands.add(startTime);
        commands.add("-i");
        commands.add(inputFile);
        commands.add("-t");
        commands.add(endTime);
        commands.add("-codec");
        commands.add("copy");
        commands.add("-y");
        commands.add(outFile);

        AvExecutor.runCommand(commands);
    }

    /**
     * ffmpeg -i 1.mp4 -vframes 1 -ss 0 -y 1.jpg
     *
     * @param inputFile
     * @param fileType
     */
    public static void createImg(String inputFile, String fileType) {

        boolean slashLeft = inputFile.contains("/");
        //切割路径
        String[] slashArr = null;
        String cutPath = "";
        if (slashLeft) {
            slashArr = inputFile.split("/");
            cutPath = "/cut/";
        } else {
            slashArr = inputFile.split("\\\\");
            cutPath = "\\cut\\";
        }

        String outFileName = slashArr[slashArr.length - 1];
        //拼接新文件名称
        String fileName = String.valueOf(outFileName).split("\\.")[0] + "." + fileType;

        //组装输出路径
        String outPath = inputFile.replace(outFileName, "");

        //去掉最后一个斜杠或反斜杠
        String newPath = !slashLeft ? outPath.substring(0, outPath.lastIndexOf("\\"))
                : outPath.substring(0, outPath.lastIndexOf("/"));

        //输出文件包含目录
        String cutDirs = newPath + cutPath;

        //创建文件目录,不存在也创建
        FileUtil.mkDirs(cutDirs);

        String outFile = cutDirs + fileName;

        List<String> commands = new ArrayList<>();
        commands.add(ffmpegCommand);
        commands.add("-i");
        commands.add(inputFile);
        commands.add("-vframes");
        commands.add("1");
        commands.add("-ss");
        commands.add("250");
        commands.add("-y");
        commands.add(outFile);

        AvExecutor. runCommand(commands);
    }

    /**
     * ffmpeg -i 1.mp4 -filter_complex "delogo=x=56:y=33:w=247:h=77" -y 0.mp4
     *
     * @param inputFile
     * @TODO 此方法有卡顿
     */
    public static void delogo(String inputFile, int x, int y, int w, int h) {

        boolean slashLeft = inputFile.contains("/");
        //切割路径
        String[] slashArr = null;
        String cutPath = "";
        if (slashLeft) {
            slashArr = inputFile.split("/");
            cutPath = "/delogo/";
        } else {
            slashArr = inputFile.split("\\\\");
            cutPath = "\\delogo\\";
        }

        String outFileName = slashArr[slashArr.length - 1];

        //组装输出路径
        String outPath = inputFile.replace(outFileName, "");

        //去掉最后一个斜杠或反斜杠
        String newPath = !slashLeft ? outPath.substring(0, outPath.lastIndexOf("\\"))
                : outPath.substring(0, outPath.lastIndexOf("/"));

        //输出文件包含目录
        String cutDirs = newPath + cutPath;

        //创建文件目录,不存在也创建
        FileUtil.mkDirs(cutDirs);

        String outFile = cutDirs + outFileName;

        List<String> commands = new ArrayList<>();
        commands.add(ffmpegCommand);
        commands.add("-i");
        commands.add(inputFile);
        commands.add("-filter_complex");
        commands.add("\"delogo=x=" + x + ":y=" + y + ":w=" + w + ":h=" + h + "\"");
        commands.add("-y");
        commands.add(outFile);

        AvExecutor.runCommand(commands);
    }

}
